package com.iwhalecloud.bss.kite.cucc.common.constant;

import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.constant.IErrorConsts;
import com.iwhalecloud.bss.kite.cucc.common.cache.LocalErrorMsgCache;
import com.ztesoft.bss.common.util.I18nUtil;
import com.ztesoft.common.logger.ErrorCode;
import org.apache.commons.lang3.StringUtils;

import java.text.MessageFormat;
import java.util.*;

/**
 * 核心版本: 规范异常编码段：40341500-40342999<br>
 * 编号原则: 按类别区分，100一个类别段
 * 如果定制包不想做国际化标准配置(详见/gd-kite-common/src/main/resources/i18n),或者想覆盖掉国际化标准配置,则需要在local_kite_error_msg表里配置错误信息描述
 * 两个地方至少要配置一处才能正常显示
 */
public enum LocalErrorConsts implements IErrorConsts {

    // 请求参数【{0}】错误{1}
    BSS_KITE_LOCAL_PARAM_ERR("BSS_KITE_LOCAL_PARAM_ERR_00001", "BSS_KITE_LOCAL_PARAM_ERR_00001"),

    ZONGYUN_CODE_ERROR("40344107", "BSS_KITE_INF_41669"),

    // 当前营业员【{0}】没有【{1}】权限
    BSS_KITE_LOCAL_PRIV_ERR("BSS_KITE_LOCAL_PRIV_ERR_10001", "BSS_KITE_LOCAL_PRIV_ERR_10001"),

    EMPTY("", ""),

    /**
     * 业务变更异常
     */
    BUSINESS_CHANGE_ERROR01("BUSINESS_CHANGE_ERROR01", "BUSINESS_CHANGE_ERROR01"),
    /**
     * 合同信息不可为空
     */
    DELIVERYINFO_NOT_NULL("DELIVERYINFO_NOT_NULL", "DELIVERYINFO_NOT_NULL"),

    /**
     * 付费关系不可为空
     */
    ACCT_REL_NOT_NULL("ACCT_REL_NOT_NULL", "ACCT_REL_NOT_NULL"),

    /**
     * 收货信息不可为空
     */
    CONTRACT_NOT_NULL("CONTRACT_NOT_NULL", "CONTRACT_NOT_NULL"),

    /**
     * 省份为空
     */
    PROVINCE_IS_EMPTY("PROVINCE_IS_EMPTY", "PROVINCE_IS_EMPTY"),

    /**
     * 地市为空
     */
    EPARCHY_IS_EMPTY("EPARCHY_IS_EMPTY", "EPARCHY_IS_EMPTY"),

    /**
     * 必填属性不可为空
     */
    ATTR_NOT_NULL("ATTR_NOT_NULL", "ATTR_NOT_NULL"),

    /**
     * 必填属性不可为空
     */
    CITY_NOT_EQUAL("CITY_NOT_EQUAL", "CITY_NOT_EQUAL"),
    /**
            * 业务类型不可能为空
     */
    BUSINESS_TYPE_NOT_NULL("BUSINESS_TYPE_NOT_NULL", "BUSINESS_TYPE_NOT_NULL"),

    /**
     * 资费时间不连续
     */
	NO_SUCCESSIVE("NO_SUCCESSIVE", "NO_SUCCESSIVE"),

	GOODS_IS_NULL("GOODS_IS_NULL", "GOODS_IS_NULL"),

    /**
     * 默认付费账户不可为空
     */
    DEFAULT_ACCT_REL_ONLY("DEFAULT_ACCT_REL_ONLY", "DEFAULT_ACCT_REL_ONLY"),

    /**
     * 白名单校验
     */
    UPLOAD_FILE_ERR_602("40316602", "BSS_KITE_ATTR_43081"),
    UPLOAD_FILE_ERR_603("40316603", "BSS_KITE_ATTR_43082"),
    UPLOAD_FILE_ERR_604("40316604", "BSS_KITE_ATTR_43083"),

    BSS_KITE_ATTR_43084("40316605", "BSS_KITE_ATTR_43084"),

    /**
     * 绿色工单校验
     */
    BSS_KITE_ATTR_43085("40316606", "BSS_KITE_ATTR_43085"),
    BSS_KITE_ATTR_43086("40316607", "BSS_KITE_ATTR_43086"),

    BSS_KITE_BATCH_50001("40316701", "BSS_KITE_BATCH_50001"),
    BSS_KITE_BATCH_50002("40316702", "BSS_KITE_BATCH_50002"),
    BSS_KITE_BATCH_50003("40316703", "BSS_KITE_BATCH_50003"),
    BSS_KITE_BATCH_50004("40316704", "BSS_KITE_BATCH_50004"),

    /**
     * 创建账户失败
     */
    CERT_NUM_INVALID("CERT_NUM_INVALID", "CERT_NUM_INVALID");

    private final String code;

    private final String msgCode;

    private final ErrorCode errorCode;






    LocalErrorConsts(String code, String msgCode) {
        this.code = code;
        this.msgCode = msgCode;
        this.errorCode = new ErrorCode(code);
    }

    public String getLocalCode() {
        return code;
    }

    @Override
    public String getCode() {
        return LocalErrorMsgCache.getErrCode(getLocalCode());
    }

    @Override
    public String getMsgCode() {
        return msgCode;
    }

    @Override
    public ErrorCode getErrorCode() {
        return errorCode;
    }

    @Override
    public String getMsg(Object... args) {
        String errMsg = null;
        try {
            errMsg = LocalErrorMsgCache.getErrMsg(getLocalCode());
        }
        catch (Exception | ExceptionInInitializerError e) {
            // ignore
        }

        if (StringUtils.isNotBlank(errMsg)) {
            // 使用数据库配置的信息覆盖配置文件的
            return MessageFormat.format(errMsg, args);
        }
        else {
            // 如果数据库没有配置,则根据msgCode查国际化配置文件进行替换
            return StringUtils.isNotBlank(getMsgCode()) ? I18nUtil.getMessage(getMsgCode(), args) : "";
        }
    }
    public static List<Map<String,Object>> getErrorConsts(){
        List<Map<String,Object>> list = new ArrayList<>();
        Arrays.stream(ErrorConsts.values()).forEach(code->{
            Map<String,Object> map = new HashMap<>();
            map.put(code.name(),code.getCode());
            map.put("codeFailMsg",getFailMsg(code.getMsgCode()));
            list.add(map);
        });
        return list;
    }
    private static String getFailMsg(String codeFailMsg) {
        String i18nUtilFailMsg = I18nUtil.getMessageWithoutLog(codeFailMsg);
        i18nUtilFailMsg = org.apache.commons.lang.StringUtils.isEmpty(i18nUtilFailMsg) ? codeFailMsg : i18nUtilFailMsg;
        return i18nUtilFailMsg;
    }
}
